<?php

// 使用函数完成期末任务
//
/**
 * 获得指定仓库的提交信息
 * @param  string $url 仓库地址，默认每次200个
 * @return array      以数组形式返回提交的信息
 */

function getCommit($url) {

    // 初始化curl工具
    $ch = curl_init($url);

    // 进行必要的配置
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // 取消curl显示返回内容
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    // 执行curl
    $data = curl_exec($ch);
    // 对data进行解码，将其从json格式转化为php数组
    $data = json_decode($data, true);
    curl_close($ch);
    return $data;
}

// 使用函数输出每周作业提交次数
function getCommitByWeek(array $data) {

// 提取每一次作业的提交时间
    $commitDate = array();
    foreach ($data as $key => $value) {
        $commitDate[$key] = strtotime($data[$key]['commit']['commiter']['date']);
    }
    return $commitDate;
}

/**
 * 生成学期周次节点
 * @param  string $start 课程开始日期
 * @return array         包含19周上课时间节点的数组
 */
function weekly($start = '2018-03-05') {
    for ($i = 0; $i < 19; $i++) {
        $data[$i] = strtotime("+" . $i . " week", strtotime($start));
    }
    return $data;
}

/**
 * 获取每周作业推送次数
 * @param  array  $commits 学生作业推送信息数组
 * @return array           学生每周作业推送次数
 */
function getCountByWeek(array $commitDate = array()) {
    // 先得到学期时间节点
    $week = weekly();
    // 初始化数组
    $array = array();
    // 逐一比较
    for ($i = 0; $i < 19; $i++) {
        $array[$i] = 0;
        foreach ($commitDate as $key => $value) {
            if ($value >= $week[$i] && $value <= $week[$i + 1]) {
                $array[$i]++;
            }
        }
    }
    return $array;
}

/**
 * 生成作业总报告
 * @param  array $data 全部提交信息
 * @return void
 */
function report(array $data, array $countByWeek) {
    utf8();
    echo '我的作业地址是：' . '<br>';
    echo '提交次数：' . count($data) . '次。<br>';
    $num = 0;
    $key = 0;
    foreach ($countByWeek as $key => $value) {
        echo '第' . ($key + 1) . '周提交作业' . $countByWeek[$key] . '次。<br>';
        if($value > 0)
        {
            $num++;
        }
    }
    echo '总共提交了'.$num.'次。<br>';
    if($num*10 > 100)
        $num = 100;
    echo '最后期末得分'.($num*10).'分。<br>';
}

// 设置页面编码为utf-8
function utf8() {
    header("Content-type:text/html;charset=utf-8");
}
// 取得指定仓库的提交信息
$data = getCommit('https://gitee.com/api/v5/repos/DengTianJin/learningphp/commits?page=1&per_page=200');
// var_dump($data);exit;
$commitDate  = getCommitByWeek($data);
$countByWeek = getCountByWeek($commitDate);
$num = 0;
//report($data, $countByWeek);
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"> 
    <title>PHP期末作业</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">  
    <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style type="text/css">
        #app-1{
            width: 50%;
            margin:auto;
            <!--margin-top: 6%;-->
        }
        .btn{
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="container">
      <div class="row">
         <div class="col-md-8  col-md-offset-2">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                    <h1 class="panel-title  text-center">学生作业详细信息</h1>
                    </div>
                    <div class="panel-body">
                        <table class="table table-striped">
                            <tr>
                                <td width="50%">学生姓名</td>
                                <td >邓天金</td>
                            </tr>
                            <tr>
                               <td> 学号 </td>
                                <td>P161813680</td>
                            </tr>
                            <tr>
                                <td>作业地址</td>
                                <td><a href="https://gitee.com/DengTianJin/learningphp.git">https://gitee.com/DengTianJin/learningphp.git</a></td>
                            </tr>
                            <tr>
                                <td>仓库托管站点</td>
                                <td> gitee.com </td>
                            </tr>
                            <tr>
                                <td>用户名</td>
                                <td>DengTianJin</td>
                            </tr>

                            <tr>
                                <td>仓库名</td>
                                <td>learningphp</td>
                            </tr>
                           
                             <tr>
                                <td>作业提交次数</td>
                                <td><?php echo count($data) . '次<br>'; ?></td>
                            </tr>   

                            
                           <?php foreach ($countByWeek as $key => $value): ?>
                            <tr>
                            <td><?php echo '第'.($key + 1).'周作业提交情况';
                             if($value > 0)
                            {
                                $num++;
                            }
                             ?></td>
                            <td><?php echo $countByWeek[$key]?></td>                 
                             </tr>
                            <?php endforeach?>

                             <tr>
                                <td>总共成功提交次数</td>
                                <td><?php echo $num; ?></td>
                            </tr> 
                            <tr>
                                <td>平时成绩</td>
                                <td><?php 
                                if($num > 10)
                                    $num = 10;
                                echo ($num*10).'分'; ?></td>
                            </tr>   
                        </table>
                    </div>               
            </div>
        </div>
</div>
</body>
</html>